/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.muni.fi.pv168.reminder.swing;

import cz.muni.fi.pv168.reminder.swing.workers.GetTaskSwingWorker;
import cz.muni.fi.pv168.reminder.entities.Task;
import cz.muni.fi.pv168.reminder.entities.Worker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractListModel;
import cz.muni.fi.pv168.reminder.swing.workers.GetWorkersOfTaskSwingWorker;

/**
 *
 * @author Adic
 */
public class DialogTaskDetail extends javax.swing.JDialog {
    private Long taskID;
    private Task task;

    @Override
    public void setVisible(boolean b) {
        if(task!=null || !b){
            super.setVisible(b); 
        }        
    }
    
    private GetTaskSwingWorker taskLoadSwingWorker;
    private GetWorkersOfTaskSwingWorker getWorkersOfTaskSwingWorker;
    
    /**
     * Creates new form DialogTaskDetail
     * @param parent
     * @param modal
     * @param taskID
     */
    public DialogTaskDetail(java.awt.Frame parent, boolean modal, Long taskID) {
        super(parent, modal);
        this.taskID = taskID;
        initComponents();
        loadData();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialogError = new javax.swing.JDialog();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabelName = new javax.swing.JLabel();
        jLabelDeadline = new javax.swing.JLabel();
        jLabelState = new javax.swing.JLabel();
        jLabelDescription = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaDesc = new javax.swing.JTextArea();
        jButtonExit = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaName = new javax.swing.JTextArea();
        jButtonSave = new javax.swing.JButton();
        jRadioButtonDone = new javax.swing.JRadioButton();
        jRadioButtonNotDone = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        jScrollPaneWorkersOfTask = new javax.swing.JScrollPane();
        jListWorkers = new javax.swing.JList();
        jButtonAddWorker = new javax.swing.JButton();
        jButtonRemoveWorkers = new javax.swing.JButton();
        jXDatePicker1 = new org.jdesktop.swingx.JXDatePicker();

        jDialogError.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/reminder/swing/Strings"); // NOI18N
        jDialogError.setTitle(bundle.getString("ERROR")); // NOI18N
        jDialogError.setModal(true);
        jDialogError.setType(java.awt.Window.Type.UTILITY);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(bundle.getString("JLABEL1")); // NOI18N

        jButton1.setText(bundle.getString("OK")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(131, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(131, 131, 131))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jLabel1)
                .addGap(37, 37, 37)
                .addComponent(jButton1)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jDialogErrorLayout = new javax.swing.GroupLayout(jDialogError.getContentPane());
        jDialogError.getContentPane().setLayout(jDialogErrorLayout);
        jDialogErrorLayout.setHorizontalGroup(
            jDialogErrorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jDialogErrorLayout.setVerticalGroup(
            jDialogErrorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(bundle.getString("TASK DETAIL")); // NOI18N

        jLabelName.setText(bundle.getString("NAME")); // NOI18N

        jLabelDeadline.setText(bundle.getString("DEADLINE")); // NOI18N

        jLabelState.setText(bundle.getString("STATE")); // NOI18N

        jLabelDescription.setText(bundle.getString("DESCRIPTION")); // NOI18N

        jTextAreaDesc.setColumns(20);
        jTextAreaDesc.setFont(jTextAreaDesc.getFont().deriveFont(jTextAreaDesc.getFont().getSize()+2f));
        jTextAreaDesc.setLineWrap(true);
        jTextAreaDesc.setRows(5);
        jTextAreaDesc.setText("Task description Task description Task description Task description Task description Task description Task description Task description \n");
        jTextAreaDesc.setWrapStyleWord(true);
        jScrollPane1.setViewportView(jTextAreaDesc);

        jButtonExit.setText(bundle.getString("EXIT")); // NOI18N
        jButtonExit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonExitMouseClicked(evt);
            }
        });
        jButtonExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExitActionPerformed(evt);
            }
        });

        jTextAreaName.setColumns(20);
        jTextAreaName.setFont(jTextAreaName.getFont().deriveFont(jTextAreaName.getFont().getSize()+2f));
        jTextAreaName.setLineWrap(true);
        jTextAreaName.setRows(2);
        jTextAreaName.setText(bundle.getString("TASK NAME")); // NOI18N
        jTextAreaName.setWrapStyleWord(true);
        jTextAreaName.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(240, 240, 240)));
        jTextAreaName.setCaretColor(new java.awt.Color(240, 240, 240));
        jScrollPane2.setViewportView(jTextAreaName);

        jButtonSave.setText(bundle.getString("SAVE")); // NOI18N
        jButtonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveActionPerformed(evt);
            }
        });

        jRadioButtonDone.setSelected(true);
        jRadioButtonDone.setText(bundle.getString("DONE")); // NOI18N
        jRadioButtonDone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonDoneActionPerformed(evt);
            }
        });

        jRadioButtonNotDone.setText(bundle.getString("IN PROGRESS")); // NOI18N
        jRadioButtonNotDone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonNotDoneActionPerformed(evt);
            }
        });

        jLabel5.setText(bundle.getString("WORKERS")); // NOI18N

        jListWorkers.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jListWorkers.setCellRenderer(new WorkerCellRenderer());
        jListWorkers.setModel(new WorkerListModel());
        jScrollPaneWorkersOfTask.setViewportView(jListWorkers);

        jButtonAddWorker.setText(bundle.getString("ADD WORKER")); // NOI18N
        jButtonAddWorker.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddWorkerActionPerformed(evt);
            }
        });

        jButtonRemoveWorkers.setText(bundle.getString("REMOVE SELECTED WORKERS")); // NOI18N
        jButtonRemoveWorkers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveWorkersActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelState, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelDeadline, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelName, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 781, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jXDatePicker1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jRadioButtonNotDone)
                                        .addGap(18, 18, 18)
                                        .addComponent(jRadioButtonDone))))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 661, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPaneWorkersOfTask)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButtonAddWorker, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jButtonExit, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jButtonSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButtonRemoveWorkers, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelName)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelState)
                    .addComponent(jRadioButtonDone)
                    .addComponent(jRadioButtonNotDone))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDeadline)
                    .addComponent(jXDatePicker1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabelDescription)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 451, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPaneWorkersOfTask)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonAddWorker)
                            .addComponent(jButtonRemoveWorkers))))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonExit)
                    .addComponent(jButtonSave))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonExitMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonExitMouseClicked
 
    }//GEN-LAST:event_jButtonExitMouseClicked

    private void jButtonExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExitActionPerformed
        setVisible(Boolean.FALSE);
    }//GEN-LAST:event_jButtonExitActionPerformed

    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed
        if(jTextAreaName.getText().length() == 0){
            jLabel1.setText(java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/reminder/swing/Strings").getString("PLEASE INSERT NAME."));
            jDialogError.pack();
            jDialogError.setModal(Boolean.TRUE);
            jDialogError.setVisible(Boolean.TRUE);
            
            return;
        }
        task.setName(jTextAreaName.getText());
        task.setDescription(jTextAreaDesc.getText());
        task.setDone(Boolean.FALSE);
        
        if(jXDatePicker1.getDate() == null){
            jLabel1.setText(java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/reminder/swing/Strings").getString("PLEASE INSERT CORRECT DATE."));
            jDialogError.pack();
            jDialogError.setModal(Boolean.TRUE);
            jDialogError.setVisible(Boolean.TRUE);
            
            return;
        }
        task.setDeadline(jXDatePicker1.getDate());
        task.setDone(jRadioButtonDone.isSelected());
        
        setVisible(false);
    }//GEN-LAST:event_jButtonSaveActionPerformed

    private void jButtonAddWorkerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddWorkerActionPerformed
        DialogWorkerList workerListDialog = new DialogWorkerList(null, true,taskID);
        workerListDialog.pack();
        workerListDialog.setVisible(Boolean.TRUE);
        List<Worker> workerList;
        workerList = workerListDialog.getWorkers();
        if(workerList!=null)
            ((WorkerListModel) jListWorkers.getModel()).addWorkers(workerList);        
    }//GEN-LAST:event_jButtonAddWorkerActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        jDialogError.setVisible(Boolean.FALSE);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jRadioButtonNotDoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonNotDoneActionPerformed
        jRadioButtonDone.setSelected(Boolean.FALSE);
        jRadioButtonNotDone.setSelected(Boolean.TRUE);
    }//GEN-LAST:event_jRadioButtonNotDoneActionPerformed

    private void jRadioButtonDoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonDoneActionPerformed
        jRadioButtonDone.setSelected(Boolean.TRUE);
        jRadioButtonNotDone.setSelected(Boolean.FALSE);
    }//GEN-LAST:event_jRadioButtonDoneActionPerformed

    private void jButtonRemoveWorkersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveWorkersActionPerformed
        ((WorkerListModel) jListWorkers.getModel()).removeWorkers(jListWorkers.getSelectedValuesList());
    }//GEN-LAST:event_jButtonRemoveWorkersActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonAddWorker;
    private javax.swing.JButton jButtonExit;
    private javax.swing.JButton jButtonRemoveWorkers;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JDialog jDialogError;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabelDeadline;
    private javax.swing.JLabel jLabelDescription;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JLabel jLabelState;
    private javax.swing.JList jListWorkers;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRadioButtonDone;
    private javax.swing.JRadioButton jRadioButtonNotDone;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPaneWorkersOfTask;
    private javax.swing.JTextArea jTextAreaDesc;
    private javax.swing.JTextArea jTextAreaName;
    private org.jdesktop.swingx.JXDatePicker jXDatePicker1;
    // End of variables declaration//GEN-END:variables

    private void loadData() {
        task = null;

        taskLoadSwingWorker = new GetTaskSwingWorker(taskID);
        taskLoadSwingWorker.execute();
        try {
            task = taskLoadSwingWorker.get();
        } catch (InterruptedException | ExecutionException ex) {
            Logger.getLogger("Reminder").log(Level.SEVERE, "Can't load task with id: " + taskID, ex);
        }
        
        if(task==null){
            jLabel1.setText(java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/reminder/swing/Strings").getString("SORRY, CAN'T LOAD SELECTED TASK."));
            jDialogError.pack();
            jDialogError.setVisible(Boolean.TRUE);
            this.task = null;
            setVisible(Boolean.FALSE);
            
            return;
        }
        
        jTextAreaName.setText(task.getName());
        jTextAreaDesc.setText(task.getDescription());
        jRadioButtonDone.setSelected(task.getDone());
        jRadioButtonNotDone.setSelected(!task.getDone());
        jXDatePicker1.setDate(task.getDeadline());
        
        List<Worker> list = new ArrayList<>();
            
        getWorkersOfTaskSwingWorker = new GetWorkersOfTaskSwingWorker(task);
        getWorkersOfTaskSwingWorker.execute();
        try {
            list = getWorkersOfTaskSwingWorker.get();
        } catch (InterruptedException | ExecutionException ex) {
            Logger.getLogger("Reminder").log(Level.SEVERE, "Can't load workers of task: " + task, ex);
        }
         
        ((WorkerListModel) jListWorkers.getModel()).addWorkers(list);        
    }
    
    public Task getTask(){
        return task;
    }
    
    public List<Worker> getWorkers(){
        return ((WorkerListModel) jListWorkers.getModel()).getWorkers();
    }

    private static class WorkerListModel extends AbstractListModel{
        List<Worker> workers;

        @Override
        public int getSize() {
            return workers.size();
        }

        @Override
        public Object getElementAt(int index) {
            if(index < 0 || index >= workers.size())
                throw new IllegalArgumentException("index size"); //NOI18N
            return workers.get(index);
        }
        
        public WorkerListModel() {
            this.workers = new ArrayList<>();                      
        }
        
        public void addWorkers(List<Worker> newWorkers){
            for(Worker worker: newWorkers){
                if(!workers.contains(worker))
                    workers.add(worker);
            }
            fireContentsChanged(workers, 0, workers.size()-1);
        }
        
        public List<Worker> getWorkers(){
            return Collections.unmodifiableList(workers);
        }
        
        public void removeWorkers(List<Worker> workersToRemove){
            for(Worker worker: workersToRemove){
                if(workers.contains(worker))
                    workers.remove(worker);
            }
            fireContentsChanged(workers, 0, workers.size()-1);
        }
        
        
        
    }


}
